package game.util.xml;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
/**
 * A help class used to do write things in xml file.
 * 
 * @author Linzy
 * Create Date: 2009 9 7 11:07:28
 * 
 */
public class XmlHelper {
	
	/**
	 * Write a tag with specified tag name and value.<br/>
	 * Effect example:
	 * < tmplTag > value < / tmplTag >
	 *  
	 * @param buff	StringBuffer;
	 * @param tagName	String; A tag you want to use in xml. 
	 * @param value	String; The tag value.
	 */
	public static void writeTag(StringBuffer buff, String tagName, String value){
		String head = "<" + tagName + ">";
		String tail = "</" + tagName + ">";
		buff.append(head);
		buff.append(value);
		buff.append(tail);
	} // writeTag()
	
	/**
	 * Get the root element's value.
	 * 
	 * @param xmlStr String; Xml format string.
	 * @return 
	 */
	public static String getRootElemVal(String xmlStr){
		Document doc = null;
		String value = "false";
		if(xmlStr == null || xmlStr.equals("")){
			return value;
		}
		
		try {
			doc = DocumentHelper.parseText(xmlStr);
			value = doc.getRootElement().getText();
		} catch (Exception e) {
			e.printStackTrace();
			value = "false";
		}
		return value;
	} // getRootElemVal()
	
	
} // end of class
